package org.sunflower.aliyun.ice;

import com.aliyun.ice20201109.Client;
import com.aliyun.ice20201109.models.GetMediaProducingJobRequest;
import com.aliyun.ice20201109.models.GetMediaProducingJobResponse;
import com.aliyun.ice20201109.models.SubmitMediaProducingJobRequest;
import com.aliyun.ice20201109.models.SubmitMediaProducingJobResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @description: Aliyun Ice相关
 * @author: TanJingyu
 * @create: 2021-10-11 19:17
 **/
@Service
@Slf4j
@RequiredArgsConstructor
public class IceService {
    private final Client client;

    /**
     * 通过模板创建合成任务
     * @param templateId 模板ID
     * @param params 模板需要的参数
     * @return SubmitMediaProducingJobResponse 
     * @link {https://help.aliyun.com/document_detail/197853.html}
     */
    public SubmitMediaProducingJobResponse submitMediaProducingJobAdoptTemplate(String templateId, SubmitMediaProducingJobRequestParam params) throws Exception {
        SubmitMediaProducingJobRequest request = new SubmitMediaProducingJobRequest();
        request.setTemplateId(templateId);
        request.setClipsParam(params.getTemplateParam());
        request.setOutputMediaConfig(params.getOutPut());

        SubmitMediaProducingJobResponse response = null;

        log.info("开始通过模板创建合成任务，模板ID:{},模板参数json串：{}，成品输出位置json串：{}", templateId, params.getTemplateParam(), params.getOutPut());
        try {
            response = client.submitMediaProducingJob(request);
            log.info("通过模板创建合成任务成功");
        } catch (Exception e) {
            log.error("通过模板创建合成任务失败！");
            throw e;
        }

        return response;
    }

    /**
     * 通过任务ID获取此合成任务详细信息
     * @param jobId 任务ID
     * @return GetMediaProducingJobResponse
     * GetMediaProducingJobResponse.getBody().getMediaProducingJob().getStatus()
     * -Init （初始状态）
     * -Queuing（排队中）
     * -Processing（处理中）
     * -Success（成功）
     * -Failed（失败）
     * @link {https://help.aliyun.com/document_detail/197854.html?spm=5176.20963767.help.dexternal.685d3ad76fTPcL}
     */
    public GetMediaProducingJobResponse getMediaProducingJobResult(String jobId) throws Exception {
        GetMediaProducingJobRequest request = new GetMediaProducingJobRequest();
        request.setJobId(jobId);
        GetMediaProducingJobResponse response = null;
        try {
            log.info("获取任务ID为[{}]的任务信息", jobId);
            response = client.getMediaProducingJob(request);
        } catch (Exception e) {
            log.error("获取任务ID为[{}]的任务信息失败！", jobId);
            throw e;
        }

        return response;
    }

}
